/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.edmo.fastener.slave;

import com.edmo.fastener.business.CryptoKeyGenerator;
import com.edmo.fastener.business.ListofCustomersnSuppliers;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.Cipher;
import javax.crypto.SealedObject;
import javax.crypto.SecretKey;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author obelog
 */
public class OrderGenerator implements Runnable {

    private String[] material = new String[]{"Metal", "Plastic"};
    private String[] delivery = new String[]{"P", "D"};
    private ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
    private IocBean dir = (IocBean) context.getBean("folder");
    private static Logger log = Logger.getLogger(OrderGenerator.class.getName());
    private static SecretKey key;

    public static void main(String[] args) {
        new OrderGenerator().fire(5);
    }

    public void run() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    private void generateOrder(String path, String name) {
        try {
            File file = new File(path + "\\" + name);
            CryptoKeyGenerator.writeToFile(file.toString(), enCrypto(generator().toString()));
        } catch (Exception ex) {
            Logger.getLogger(OrderGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private StringBuilder generator() throws IOException {
        StringBuilder sb = new StringBuilder();
        int count = (int) (Math.random() * 100 + 1);
        for (int i = 1; i < count; i++) {
            int mat = (int) (Math.random() * 2);
            int deliv = (int) (Math.random() * 2);
            String content = (int) (Math.random() * 999999 + 1) + "|" + (int) (Math.random() * 10000 + 1) + "|" + material[mat] + "|" + delivery[deliv] + "\n";
            sb.append(content);
        }
        return sb;
    }
    
    private SealedObject enCrypto(String data){
        SealedObject sealedObject = null;
        try {
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            sealedObject = new SealedObject(data, cipher);
        } catch (Exception ex) {
            Logger.getLogger(OrderGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }
        return sealedObject;
    }

    public void fire(int count) {
        key = CryptoKeyGenerator.generateKey();
        for (int k = 1; k < 1+count; k++) {
            int i = (int) (Math.random() * 200 + 1);
            String root = dir.getName() + "\\CUSTOMERS\\" + "C" + i;
            generateOrder(root, "order" + k + ".txt");
            log.info(dir.getName() + "\\CUSTOMERS\\" + "C" + i + "\\order" + k + ".txt");
        }

    }
}
